﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Data;

namespace HangameAuto02
{
    public partial class AutoForm02 : BaseForm
    {
        private short m_HotkeyID01;
        private short m_HotkeyID02;
        private short m_HotkeyID03;
        private GameKind _gameKind;

        public Thread _thread01;

        public AutoForm02()
        {
            InitializeComponent();
        }

        private void chkTop_CheckedChanged(object sender, EventArgs e)
        {
            this.TopMost = this.chkTop.Checked;
        }

        private void AutoForm02_Load(object sender, EventArgs e)
        {
            string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + this.GetType().FullName + "01";
            string atomName2 = Thread.CurrentThread.ManagedThreadId.ToString("X8") + this.GetType().FullName + "02";
            m_HotkeyID01 = NativeMethods.GlobalAddAtom(atomName);
            m_HotkeyID02 = NativeMethods.GlobalAddAtom(atomName2);
            NativeMethods.RegisterHotKey(Handle, m_HotkeyID01, 0, (int)Keys.F2);
            NativeMethods.RegisterHotKey(Handle,m_HotkeyID02, 0, (int)Keys.F3);

            this.SetDrop01();
        }

        private void SetDrop01()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("VALUE");
            dt.Columns.Add("TEXT");
            dt.Rows.Add(new object[] {"1", "채널입장"});
            dt.Rows.Add(new object[] { "2", "무한참여" });
            dt.Rows.Add(new object[] { "3", "무한클릭" });
            dt.AcceptChanges();
            this.cboGB01.DataSource = dt;
            this.cboGB01.DisplayMember = "TEXT";
            this.cboGB01.ValueMember = "VALUE";
        }

        private void cboGB01_SelectedIndexChanged(object sender, EventArgs e)
        {

            switch (this.cboGB01.SelectedIndex)
            {
                case 0:
                    DataTable dt = new DataTable();
                    dt.Columns.Add("VALUE");
                    dt.Columns.Add("TEXT");
                    dt.Rows.Add(new object[] {"1", "채널1"});
                    dt.Rows.Add(new object[] {"2", "채널2"});
                    dt.Rows.Add(new object[] {"3", "채널3"});
                    dt.Rows.Add(new object[] {"4", "채널4"});
                    dt.Rows.Add(new object[] {"5", "채널5"});
                    dt.Rows.Add(new object[] {"6", "채널6"});
                    dt.Rows.Add(new object[] {"7", "채널7"});
                    dt.Rows.Add(new object[] {"8", "채널8"});
                    dt.Rows.Add(new object[] {"9", "채널9"});
                    dt.Rows.Add(new object[] {"10", "채널10"});
                    dt.Rows.Add(new object[] {"11", "채널11"});
                    dt.Rows.Add(new object[] {"12", "채널12"});
                    dt.Rows.Add(new object[] {"13", "채널13"});
                    dt.Rows.Add(new object[] {"14", "채널14"});
                    dt.Rows.Add(new object[] {"15", "채널15"});
                    dt.Rows.Add(new object[] {"16", "채널16"});
                    dt.Rows.Add(new object[] {"17", "채널17"});
                    dt.Rows.Add(new object[] {"18", "채널18"});
                    dt.Rows.Add(new object[] {"19", "채널19"});
                    dt.Rows.Add(new object[] {"20", "채널20"});
                    dt.AcceptChanges();
                    this.cboGB02.DataSource = dt;
                    this.cboGB02.DisplayMember = "TEXT";
                    this.cboGB02.ValueMember = "VALUE";
                    break;
                case 1:
                    this.cboGB02.DataSource = null;
                    break;
                case 2:
                    this.cboGB02.DataSource = null;
                    break;

            }
        }



        public void BaseForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            NativeMethods.UnregisterHotKey(Handle, m_HotkeyID01);
            NativeMethods.GlobalDeleteAtom(m_HotkeyID01);

            NativeMethods.UnregisterHotKey(Handle, m_HotkeyID02);
            NativeMethods.GlobalDeleteAtom(m_HotkeyID02);

            if (this._thread01 != null)
                this._thread01.Abort();

            Dispose();
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message msg)
        {

            switch (msg.Msg)
            {
                case Consts.WM_HOTKEY:
                    if ((short)msg.WParam == m_HotkeyID01)
                    {
                        Common.SetGameProcess();
                        switch (this.cboGB01.SelectedIndex)
                        {
                            case 0:
                                break;
                            case 1:
                                break;
                            case 2:
                                base.OnMouseClickeHook = true;
                                break;
                        }
                    }
                    else if ((short) msg.WParam == m_HotkeyID02)
                    {
                        if (this._thread01 != null && this._thread01.IsAlive)
                            this._thread01.Abort();
                        this.Show();
                        
                    }
                    break;  
                default:
                    base.WndProc(ref msg);
                    break;
            }
        }

        public override void HookManager_MouseClick(object sender, MouseEventArgs e)
        {
            base.OnMouseClickeHook = false;
            this.Hide();
            this._thread01 = new Thread(new ParameterizedThreadStart(AutoClick));
            this._thread01.Start(e.Location);

        }

        private void AutoClick(object pPoint)
        {
            Common.AutoClick((Point)pPoint);
        }

    }
}
